<template>
  <div class="category-label"
       :title="categoryText">
    <a-tag :style="categoryStyle"
           @click.stop="openLink"
           class="tag-show">
      {{ categoryText }}
    </a-tag>
  </div>
</template>

<script>
  export default {
    name: 'CategoryLabel',
    props: {
      slotData: {
        type: Object,
        default: () => {}
      },
      urlParams: {
        type: Object,
        default: () => {}
      },
      text: {
        type: String,
        default: ''
      },
      isWaitList: {
        type: Boolean,
        default: false
      }
    },
    data() {
      return {
        from: '',
        openFrom: ''
      };
    },
    mounted() {
      this.getCategoryType();
    },
    computed: {
      categoryStyle() {
        const { applicationCategoryKey } = this.slotData;
        return {
          color: this.colorArray[applicationCategoryKey].fontColor,
          backgroundColor: this.colorArray[applicationCategoryKey].backgroundColor
          // opacity: this.colorArray[applicationCategoryKey].opacity
        };
      },
      categoryText() {
        return this.isWaitList ? this.text : this.text + '(' + this.from + ')';
      }
    },
    created() {
      this.colorArray = {
        1: { fontColor: 'rgba(48, 91, 249, 1)', backgroundColor: 'rgba(235, 239, 254, 1)' }, //协同
        4: { fontColor: 'rgba(0, 131, 176, 1)', backgroundColor: 'rgba(229, 242, 247, 1)' }, //公文
        6: { fontColor: 'rgba(255, 159, 0, 1)', backgroundColor: 'rgba(255, 244, 218, 1)' }, //会议
        10: { fontColor: 'rgba(149, 117, 205, 1)', backgroundColor: 'rgba(244, 241, 250, 1)' }, //调查
        19: { fontColor: 'rgba(0, 131, 176, 1)', backgroundColor: 'rgba(229, 242, 247, 1)' }, //发文
        26: { fontColor: 'rgba(90, 200, 250, 1)', backgroundColor: 'rgba(239, 242, 244, 1)' }, //综合办公
        39: { fontColor: 'rgba(38, 166, 154, 1)', backgroundColor: 'rgba(233, 246, 244, 1)' }, //三方
        81: { fontColor: 'rgba(90, 200, 250, 1)', backgroundColor: 'rgba(238, 249, 254, 1)' } //模版审批
      };
    },
    methods: {
      openLink() {
        const {
          state,
          openType,
          applicationCategoryKey: app,
          applicationSubCategoryKey: subApp
        } = this.slotData;
        let url;
        if (app == 4) {
          //表单公文
          let listType = '';
          let govdocType = subApp;
          let _resourceCode = '';
          if (state == 1) {
            //待发
            listType = 'listWaitSend';
          } else if (state == 2) {
            //已发
            listType = 'listSent';
          } else if (state == 3) {
            //待办
            listType = 'listPendingAll';
          } else if (state == 4) {
            //已办
            listType = 'listDoneAll';
          }
          if (subApp == 19 || subApp == 1) {
            govdocType = '1';
            if (window._hasResource('F20_govDocSendManage') == true) {
              _resourceCode = 'F20_govDocSendManage';
            }
          } else if (subApp == 20 || subApp == 2 || subApp == 4) {
            govdocType = '2,4';
            if (window._hasResource('F20_receiveManage') == true) {
              _resourceCode = 'F20_receiveManage';
            }
          } else if (subApp == 21 || subApp == 3) {
            govdocType = '3';
            if (window._hasResource('F20_signReport') == true) {
              _resourceCode = 'F20_signReport';
            }
          }
          if (_resourceCode != '') {
            url =
              '/govdoc/govdoc.do?method=index&govdocType=' +
              govdocType +
              '&listType=' +
              listType +
              '&_resourceCode=' +
              _resourceCode;
          }
          if (!url) {
            if (1 == state) {
              //待发
              if (window._hasResource('F20_govDocWaitSend') == true) {
                url =
                  '/govdoc/govdoc.do?method=index&listType=listWaitSendAllRoot&_resourceCode=F20_govDocWaitSend';
              }
              listType = 'listWaitSend';
            } else if (2 == state) {
              //已发
              if (window._hasResource('F20_gocDovSend') == true) {
                url =
                  '/govdoc/govdoc.do?method=index&listType=listSentAllRoot&_resourceCode=F20_gocDovSend';
              }
              listType = 'listSent';
            } else if (3 == state) {
              //待办
              if (window._hasResource('F20_govdocPending') == true) {
                url =
                  '/govdoc/govdoc.do?method=index&listType=listPendingAllRoot&_resourceCode=F20_govdocPending';
              }
              listType = 'listPendingAll';
            } else if (4 == state) {
              //已办
              if (window._hasResource('F20_govDocDone') == true) {
                url =
                  '/govdoc/govdoc.do?method=index&listType=listPDoneAllRoot&_resourceCode=F20_govDocDone';
              }
              listType = 'listDoneAll';
            }
          }
        } else if (app == 32) {
          //信息报送
          if (state == 2) {
            //已发
            url = '/info/infomain.do?method=infoReport&listType=listInfoReported';
          } else if (state == 3) {
            //待办
            url = '/info/infomain.do?method=infoAudit&listType=listInfoPending';
          } else if (state == 4) {
            //已办
            url = '/info/infomain.do?method=infoAudit&listType=listInfoDone';
          }
        } else {
          url = '/collaboration/collaboration.do?method=' + this.openFrom + '&showTab=true';
        }
        //没有链接时不可跳转
        if (!url) {
          return;
        }
        let { openFrom } = this.urlParams;
        if (!openFrom && this.urlParams.pageKey) {
          openFrom = 'pendingCenter';
        }
        if (openFrom == 'pendingCenter') {
          //待办中心
          if (openType == '4') {
            window.openCtpWindow({
              url: window._ctxPath + url + window.CsrfGuard.getUrlSurffix()
            });
          } else {
            parent.window.location.href = window._ctxPath + url + window.CsrfGuard.getUrlSurffix();
          }
        } else {
          window.location.href = window._ctxPath + url + window.CsrfGuard.getUrlSurffix();
        }
      },
      getCategoryType() {
        const { state, subState, applicationCategoryKey } = this.slotData;
        if (state == 1) {
          //待发
          // this.from = "${ctp:i18n('common.toolbar.state.darft.label')}";
          this.from = $.i18n('common.toolbar.state.darft.label');
          this.openFrom = 'listWaitSend';
        } else if (state == 2) {
          //已发
          // this.from = "${ctp:i18n('common.toolbar.state.sended.label')}";
          this.from = $.i18n('common.toolbar.state.sended.label');
          this.openFrom = 'listSent';
        } else if (state == 3) {
          //待办
          if (
            subState == 13 &&
            (applicationCategoryKey == 19 ||
              applicationCategoryKey == 20 ||
              applicationCategoryKey == 21)
          ) {
            // this.from = "${ctp:i18n('collaboration.state.13.col_zcdb')}";
            this.from = $.i18n('collaboration.state.13.col_zcdb');
            this.openFrom = 'listZcdb';
          } else {
            // this.from = "${ctp:i18n('common.toolbar.state.pending.label')}";
            this.from = $.i18n('common.toolbar.state.pending.label');
            this.openFrom = 'listPending';
          }
        } else if (state == 4) {
          //已办
          // this.from = "${ctp:i18n('common.toolbar.state.done.label')}";
          this.from = $.i18n('common.toolbar.state.done.label');
          this.openFrom = 'listDone';
        }
      }
    }
  };
</script>

<style scoped lang="scss">
  .category-label {
    width: 100%;
    white-space: nowrap;
    text-overflow: ellipsis;
    overflow: hidden;
    .tag-show {
      border-radius: 2px;
      border: none;
      cursor: pointer;
    }
  }
</style>
